Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Never return stored content from content_for when a block is given #2794

Closed
wants to merge 2 commits into from
Closed

Never return stored content from content_for when a block is given #2794

wants to merge 2 commits into from

Conversation

tomstuart
Copy link
Contributor

Using content_for in Rails 3.0.x can result in a bogus deprecation warning ("<% %> style block helpers are deprecated") because of a bug in how content_for works. content_for should only return stored content when no block has been provided, but instead it returns stored content when the block generates a blank buffer and has a nil return value, which is what happens in a situation like this:

<% content_for :foo do %>
  <% if something_falsy %>
    foo
  <% end %>
  <% if something_else_falsy %>
    bar
  <% end %>
<% end %>

Ultimately this is because capture returns nil (vs '') when the resulting buffer is blank?, but that's apparently intended behaviour, so it's better to fix content_for to explicitly check block_given? rather than relying on the truthiness of capture(&block).

The capture helper may return nil when evaluation of the block has
produced a buffer which contains only whitespace, but that doesn't
mean content_for should return stored content. (Concretely, this
makes <% content_for :foo do %><% end %> trigger a bogus
deprecation warning if :foo already has stored content.)
@josevalim
Copy link
Contributor

Thanks, but we are no longer maintaining 3-0-stable as 3.1 is out. If this issue still persists on 3.1 though, we will apply these patches gladly.

@josevalim josevalim closed this Sep 1, 2011
@tomstuart
Copy link
Contributor Author

The bug's still there in 3.1 (even though the deprecation warning isn't), so I've opened #2799 against 3-1-stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants